<?php
/**
 * 邀请管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/9/15
 * Time: 下午6:34
 */

namespace app\control\controller;

use app\common\model\InviteModel;
use app\common\model\MemberModel;
use think\facade\Request;

class InviteController extends AdminBaseController
{
    /**
     * 推荐列表
     */
    public function index(InviteModel $art){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$art->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 搜索
     * @return array
     */
    private function search($data){

        $where=[];

        if(!empty($data["mobile"])){
            $u=MemberModel::where('mobile',$data["mobile"])
                ->field('uid')
                ->find();

            $where[]=["user_id",'=',$u['uid']];
        }

        if(!empty($data["invite_mobile"])){
            $u=MemberModel::where('mobile',$data["invite_mobile"])
                ->field('uid')
                ->find();
            $where[]=["invite_user_id",'=',$u->uid];
        }

        return $where;
    }


    /**
     * 删除分类
     * @param InviteModel $ap
     */
    public function del(InviteModel $ap){
        $id=Request::post("id");

        $status=$ap->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function delAll(InviteModel $ap){
        $id=Request::post("ids");

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$ap->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

}